package org.agile.grenoble.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.agile.grenoble.client.GetOrCreateUserClient;
import org.agile.grenoble.data.IUserStorage;
import org.agile.grenoble.user.User;


class myOkButtonListener implements ActionListener {

	
	UserRegistration ur = null; 
	public myOkButtonListener(UserRegistration pUr) {
		ur = pUr ;
	}

//	@Override
	public void actionPerformed(ActionEvent arg0) {
		ur.updateData();
		ur.dispose();
	}
}

public class UserRegistration extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	GetOrCreateUserClient iStorage = null ;
	String username = null ;
	String usermail = null ; 
	
	public UserRegistration(GetOrCreateUserClient storage) {
		iStorage = storage ;
		setTitle("User Registration");
		createDisplay();
		
	}
	
	
	/*
	 * LT :  add a form to store user's email.
	 */
	private void setEmail(String pMail) {
		usermail = pMail ; 
	}
	
	private void setMyName(String pName) {
		username= pName ;
	}
	
	public void updateData() {
		setMyName(yourname.getText());
		setEmail(youremail.getText());
	}
	
	private JTextField yourname = null ; 
	private JTextField youremail = null; 
	
	private void createDisplay() {
		JPanel jpanel = new JPanel() ;
		getContentPane().add(jpanel);
		jpanel.setLayout(new GridBagLayout());
		
		GridBagConstraints c = new GridBagConstraints();
        c.weightx = 1.0;
        GridBagConstraints eol = new GridBagConstraints();
        eol.weightx = 1.0;
        eol.gridwidth = GridBagConstraints.REMAINDER; ;
        
		JLabel name =  new JLabel("     NAME :          " ); 
		yourname =  new JTextField(30);
		JLabel email = new JLabel("     EMAIL:          " );
		youremail = new JTextField(30);
		
		JButton okButton = new JButton("OK") ;
		okButton.addActionListener(new myOkButtonListener(this));
		
		jpanel.add(name,c);jpanel.add(yourname,eol);
		jpanel.add(email,c);jpanel.add(youremail,eol);
		
		jpanel.add(okButton,eol);
		jpanel.setSize(300,200);
		
		pack();
		setVisible(true);
	}
	
	public User getUser() {
		User user = null ; 
		if ( username == null || usermail==null ) {
			//do nothing
			//System.out.println("User data not yet collected");
		} else {
			//user = UserFactory.getUser(username+"_"+System.currentTimeMillis(), usermail,iStorage) ;
			user = iStorage.getorCreateUser(username, usermail);
		}
		
		return user;
	}
}
